package com.neoden.notes.models;

import com.neoden.notes.Note;
import com.neoden.notes.data.NotesProvider;
import com.neoden.notes.util.SharedPreferenceKeys;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class NotesModel {
	private static final String TAG = NotesModel.class.getName();
	
	private Context mContext;
	private ContentResolver mContentResolver;
	
	long mCurrentListId;

	public NotesModel(Context context) {
		mContext = context;
		mContentResolver = mContext.getContentResolver();
		setCurrentListId(getLastListId());
	}

	// notes
	
	public Note getNote(long id) {
		Cursor cursor = mContentResolver.query(
				ContentUris.withAppendedId(
						NotesProvider.Notes.CONTENT_URI, id),
				NotesProvider.Notes.PROJECTION_FULL,
				null,
				null,
				null
				);
		
		if (cursor.moveToFirst()) {
			Note note = getNoteFromCursor(cursor);
			cursor.close();
			return note;
		}
		else 
			throw new IllegalArgumentException("Record with Id = " + id + " not found");
	}
	
	public long insertNote(Note note) {
		Uri newUri = mContentResolver.insert(
				NotesProvider.Notes.CONTENT_URI, getContentValuesFromNote(note));
		long insertId = ContentUris.parseId(newUri);
		return insertId;
	}
	
	public int updateNote(Note note) {
		int rowsAffected = mContentResolver.update(
				ContentUris.withAppendedId(NotesProvider.Notes.CONTENT_URI, note.getId()), 
				getContentValuesFromNote(note), null, null);
		
		return rowsAffected;
	}

	public void deleteNote(long id) {
		mContentResolver.delete(
				ContentUris.withAppendedId(
						NotesProvider.Notes.CONTENT_URI, id),
				null,
				null);
	}

	public Cursor getAllNotes(long listId) {
		Cursor cursor = mContentResolver.query(
        		NotesProvider.Notes.CONTENT_URI,
        		NotesProvider.Notes.PROJECTION_FULL,
        		NotesProvider.Notes.COLUMN_LIST_ID + " = " + listId,
        		null,
        		null
        		);

		return cursor;
	}
	
	// lists
	
	public Cursor getAllLists() {
		Cursor cursor = mContentResolver.query(
        		NotesProvider.Lists.CONTENT_URI,
        		NotesProvider.Lists.PROJECTION_FULL,
        		null,
        		null,
        		null
        		);

		return cursor;
	}
	
	public String getListTitle(long id) {
		Cursor cursor = mContentResolver.query(
				ContentUris.withAppendedId(
						NotesProvider.Lists.CONTENT_URI, id),
				NotesProvider.Lists.PROJECTION_FULL,
				null,
				null,
				null
				);
		
		if (cursor.moveToFirst()) {
			String title = cursor.getString(
					cursor.getColumnIndex(NotesProvider.Lists.COLUMN_TITLE));
			cursor.close();
			return title;
		}
		else 
			throw new IllegalArgumentException("Record with Id = " + id + " not found");
	}

	public long createList(String title) {
		ContentValues values = new ContentValues();
		values.put(NotesProvider.Lists.COLUMN_TITLE, title);
		
		Uri newUri = mContentResolver.insert(
				NotesProvider.Lists.CONTENT_URI, values);
		long insertId = ContentUris.parseId(newUri);
		
		return insertId;
	}
	
	public void deleteList(long id) {
		mContentResolver.delete(
				ContentUris.withAppendedId(
						NotesProvider.Lists.CONTENT_URI, id),
				null,
				null);
	}
	
	public int renameList(long id, String title) {
		ContentValues values = new ContentValues();
		values.put(NotesProvider.Lists.COLUMN_TITLE, title);

		int rowsAffected = mContentResolver.update(
				ContentUris.withAppendedId(NotesProvider.Lists.CONTENT_URI, id), 
				values, null, null);
		
		return rowsAffected;
	}
	
	public void setCurrentListId(long newListId) {
		mCurrentListId = newListId;
		
		// store to shared preferences
		SharedPreferences settings = mContext.getSharedPreferences(
				SharedPreferenceKeys.FILE, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(SharedPreferenceKeys.LAST_LIST_ID, mCurrentListId);
		editor.commit();
	}

	public long getCurrentListId() {
		return mCurrentListId;
	}
	
	private long getLastListId() {
		SharedPreferences settings = mContext.getSharedPreferences(
				SharedPreferenceKeys.FILE, 0);
		long lastListId = settings.getLong(
				SharedPreferenceKeys.LAST_LIST_ID, -1);
		
		if (lastListId == -1) {
			// get first list id available
			Cursor cursor = getAllLists();
			// TODO: consider list sorting
			if (!cursor.moveToFirst()) {
				Log.e(TAG, "No lists found");
			}
			else {
				lastListId = cursor.getLong(cursor.getColumnIndex(NotesProvider.Lists.COLUMN_ID));
			}
			cursor.close();
		}
		
		return lastListId;
	}
	
	// helpers
	private Note getNoteFromCursor(Cursor cursor) {
		Note note = new Note();

		note.setId(cursor.getLong(cursor.getColumnIndex(NotesProvider.Notes.COLUMN_ID)));
		note.setTitle(cursor.getString(cursor.getColumnIndex(NotesProvider.Notes.COLUMN_TITLE)));
		note.setNote(cursor.getString(cursor.getColumnIndex(NotesProvider.Notes.COLUMN_NOTE)));
		note.setCreatedTS(cursor.getLong(cursor.getColumnIndex(NotesProvider.Notes.COLUMN_CREATED_TS)));
		note.setModifiedTS(cursor.getLong(cursor.getColumnIndex(NotesProvider.Notes.COLUMN_MODIFIED_TS)));
		note.setListId(cursor.getInt(cursor.getColumnIndex(NotesProvider.Notes.COLUMN_LIST_ID)));
		
		return note;
	}
	
	private ContentValues getContentValuesFromNote(Note note) {
		ContentValues values = new ContentValues();
		
		values.put(NotesProvider.Notes.COLUMN_TITLE, note.getTitle());
		values.put(NotesProvider.Notes.COLUMN_NOTE, note.getNote());
		values.put(NotesProvider.Notes.COLUMN_CREATED_TS, note.getCreatedTS());
		values.put(NotesProvider.Notes.COLUMN_MODIFIED_TS, note.getModifiedTS());		
		values.put(NotesProvider.Notes.COLUMN_LIST_ID, note.getListId());
		
		return values;
	}
}
